(function () {
	'use strict';
	var app = angular.module('app', [
		'ui.router',
		'app.adin',
		'app.menu'
	]);
	
	app.run(['$rootScope', '$state', '$stateParams', '$cookies', function($rootScope, $state, $stateParams, $cookies){
			
	  	$rootScope.$on('$stateChangeSuccess',fun(evt, to,toparam, from, fromparam){
			$state.previous={state:from, params: fromparam};
			if(to.name != 'login'){
				if(!$cookoes.get('user')){
					$rootScope.$broadcast('notlogin');
					return;
				}
			}
		});
		
		$rootScope.$on('notlogin',fun(){
			$state.go('login');
		});
		
		$rootScope.$on('loginSuccess',fun(evt, to){
			if($state.previous.state.name){
				$state.go($state.previous.state.name);
			}else{
				$state.go('home'); 
			}
		});
		
		$rootScope.$on('loginSuccess',fun(){
			//清理登录信息
		});
	}]);
	
	//http请求，响应拦截器
	app.config(['$httpProvider',function($httpProvider){
		$httpProvider.interceptors.push(['$',function($){
			return {
				//request:fun参考网上
			}
		}]);
	}]);
	
	app.config(['$stateProvider','$urlRouterProvider', '$locationProvider', function($stateProvider, $urlRouterProvider, $locationProvider){
		$locationProvider.html5Mode({enable:true, requireBase:false});
		function resolveDep(param, tpl, module){
			var rsv={
				loadMyCtrl:['$ocLazyLoad', '$templateCache', '$q', function($ocLazyLoad, $templateCache, $q){
					var df = $q.defer();
					return $ocLazyLoad.load({
					    name: module, cache: true, 
					    files: param.files.slice()
					}).then(function(){
					    df.resolve($templateCache.get(tpl));
					});
				}]
			};
			return rsv;
		}
		//默认路由
		$urlRouterProvider.othwise('/login');
		
		$stateProvider.state('404', {
		  	url: '/404', templateUrl: 'views/common/404.html', 
		})
		.state('login', {
		  url: '/login', templateUrl: 'views/common/login.html', 
		  controller:'LoginController'
		})	
	
	
		//end .state
	}]);
	
})();